package com.ce.cases.execute.action;


import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ce.cases.execute.dao.po.CaseSourceExecute;
import com.ce.cases.hear.dao.po.CaseSourceHear;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceFiles;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.DateUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.util.FlowWorkUtil;
import com.ce.util.NetFileUtil;
import com.ce.workflow.work.dao.po.WorkflowFlow;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-04-06
* 创建时间：16:08:33
* 功能描述：执行入库情况登记 [CASE_SOURCE_CHECK]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class CaseSourceExecuteAction extends DataMaintainAction 
{
  @SuppressWarnings("unchecked")
public String exeRegPage(){
	  String caseId=this.getSelectId();
	  WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
		if(flowing==null||flowing.getVcSequence()!=4){
			return this.alertMsgAndExecJs("执行环节已不能进行该操作", "$('#operateWindow').window('close');");
		}
	  try {
		List<CaseSourceExecute> exeList=(List<CaseSourceExecute>)this.baselogic.searchByHql("From CaseSourceExecute po where po.caseId='"+caseId+"'");
		if(!exeList.isEmpty()){
			po=exeList.get(0);
		}else{
			CaseSource source=(CaseSource)this.baselogic.getPOByPk("com.ce.cases.source.dao.po.CaseSource", caseId);
			BeanAccessUtil.copyProperty(po, "caseId", caseId);
			BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
			BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	  return "exeRegPage";
  }
  public String exeReg(){
		try {
			CaseSourceExecute submitPo=(CaseSourceExecute)po;
			 if(StringUtil.isNotBlank(po.getVcId())){
				CaseSourceExecute savePo = (CaseSourceExecute) this.baselogic
						.getPOByPk(po.getClass().getName(), po.getVcId());
				savePo.setExecPerson(submitPo.getExecPerson());
				savePo.setPoliceHelp(submitPo.getPoliceHelp());
				savePo.setExecStepsNum(submitPo.getExecStepsNum());
				savePo.setExecStepsName(submitPo.getExecStepsName());
				this.baselogic.updatePo(savePo);
			  }else{
				  submitPo.setVcId(GeneratorUUID.generateUUID());
				  submitPo.setExecRegisterPerson(User.getUserName());
				  submitPo.setExecRegisterDate(new Date());
				  submitPo.setExecRegisterDeptid(User.getUserOrgId());
			    this.baselogic.addPo(submitPo);
			  }
		} catch (AppException e) {
			e.printStackTrace();
		}  
	  return this.returnInfoForJS(true, "执行情况登记成功！");
  }
  @SuppressWarnings("unchecked")
  public String inRegPage(){
	  String caseId=this.getSelectId();
	  WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(caseId, User.getUserId());//当前人正在处理的节点
		if(flowing==null||flowing.getVcSequence()!=4){
			return this.alertMsgAndExecJs("执行环节已不能进行该操作", "$('#operateWindow').window('close');");
		}
	  try {
		  
		List<CaseSourceHear> hearList = (List<CaseSourceHear>) this.baselogic
					.searchByHql("From CaseSourceHear po where po.caseId='"+ caseId + "'");
		  
		List<CaseSourceExecute> exeList=(List<CaseSourceExecute>)this.baselogic.searchByHql("From CaseSourceExecute po where po.caseId='"+caseId+"'");
		if(!exeList.isEmpty()){
			po=exeList.get(0);
		}else{
			CaseSourceHear hearPo=null;
			if(!hearList.isEmpty()){
				hearPo=hearList.get(0);
			}
			CaseSource source=(CaseSource)this.baselogic.getPOByPk("com.ce.cases.source.dao.po.CaseSource", caseId);
			BeanAccessUtil.copyProperty(po, "caseId", caseId);
			BeanAccessUtil.copyProperty(po, "caseNumber", source.getCaseNumber());
			BeanAccessUtil.copyProperty(po, "enterprice", source.getInvolvedEnterprice());
			if(hearPo!=null){
				BeanAccessUtil.copyProperty(po, "localAddedTax", hearPo.getLocalAddedTax());
				BeanAccessUtil.copyProperty(po, "localCostTax", hearPo.getLocalCostTax());
				BeanAccessUtil.copyProperty(po, "operationTax", hearPo.getOperationTax());
				BeanAccessUtil.copyProperty(po, "constructionTax", hearPo.getConstructionTax());
				BeanAccessUtil.copyProperty(po, "entIncomeTax", hearPo.getEntIncomeTax());
				BeanAccessUtil.copyProperty(po, "personalIncomeTax", hearPo.getPersonalIncomeTax());
				BeanAccessUtil.copyProperty(po, "resourceTax", hearPo.getResourceTax());
				BeanAccessUtil.copyProperty(po, "educationTax", hearPo.getEducationTax());
				BeanAccessUtil.copyProperty(po, "illegalMoney", hearPo.getIllegalMoney());
				BeanAccessUtil.copyProperty(po, "adjustFillTax", hearPo.getAdjustFillTax());
				BeanAccessUtil.copyProperty(po, "noFillTax", hearPo.getNoFillTax());
				BeanAccessUtil.copyProperty(po, "noFillbackTax", hearPo.getNoFillbackTax());
				BeanAccessUtil.copyProperty(po, "adjustIncome", hearPo.getAdjustIncome());
				BeanAccessUtil.copyProperty(po, "deductPaidTax", hearPo.getDeductPaidTax());
				BeanAccessUtil.copyProperty(po, "recoveredRefundedTax", hearPo.getRecoveredRefundedTax());
				BeanAccessUtil.copyProperty(po, "punishmentCount", hearPo.getPunishmentCount());
				BeanAccessUtil.copyProperty(po, "faKuanMoney", hearPo.getFaKuanMoney());
			}
			
			
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	  return "inRegPage";
  }
  public String inReg(){
		try {
			 CaseSourceExecute submitPo=(CaseSourceExecute)po;
			 if(StringUtil.isNotBlank(po.getVcId())){
				CaseSourceExecute oldPo = (CaseSourceExecute) this.baselogic
						.getPOByPk(po.getClass().getName(), po.getVcId());
				submitPo.setExecPerson(oldPo.getExecPerson());
				submitPo.setPoliceHelp(oldPo.getPoliceHelp());
				submitPo.setExecStepsNum(oldPo.getExecStepsNum());
				submitPo.setExecStepsName(oldPo.getExecStepsName());
				submitPo.setExecRegisterPerson(oldPo.getExecRegisterPerson());
				submitPo.setExecRegisterDate(oldPo.getExecRegisterDate());
				submitPo.setExecRegisterDeptid(oldPo.getExecRegisterDeptid());
				//如果未登记入库情况
				if(oldPo.getInRegisterDate()==null){
					submitPo.setInRegisterPerson(User.getUserName());
					submitPo.setInRegisterDate(new Date());
					submitPo.setInRegisterDeptid(User.getUserOrgId());
				}
				this.baselogic.updatePo(submitPo);
			  }else{
				submitPo.setVcId(GeneratorUUID.generateUUID());
				submitPo.setInRegisterPerson(User.getUserName());
				submitPo.setInRegisterDate(new Date());
				submitPo.setInRegisterDeptid(User.getUserOrgId());
			    this.baselogic.addPo(submitPo);
			  }
		} catch (AppException e) {
			e.printStackTrace();
		} 
	  return this.returnInfoForJS(true, "入库登记成功！");
  }
  public String showCaseSourceFile(){
		String vcId=this.getParameter("chkSelf");
		String vcType=this.getParameter("vcType");
		String vcFileId=this.getParameter("vcFileId");
		String vcFileName=this.getParameter("fileName");
		String fileType="";
		if(vcFileName.contains("."))
			fileType=vcFileName.substring(vcFileName.indexOf("."), vcFileName.length());
		this.setAttribute("fileType", fileType);
		this.setAttribute("vcFileName", vcFileName);
		String filePath="";
		try {
			filePath=NetFileUtil.getFileServerPath(vcFileId);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		this.setAttribute("filePath", filePath);
		return "showCaseSourceFile";
	}
	//上传文书
	public String checkNoticePage(){
		String vcId=this.getParameter("chkSelf");
		WorkflowFlow flowing=FlowWorkUtil.getWorkflowFlowByPersonCling(vcId, User.getUserId());//当前人正在处理的节点
		if(flowing==null||flowing.getVcSequence()!=4){
			return this.alertMsgAndExecJs("检查环节已不能进行该操作", "$('#operateWindow').window('close');");
		}
		CaseSource casePo=null;
		try {
			casePo=(CaseSource) this.baselogic.getPOByPk(CaseSource.class.getName(), vcId);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.setAttribute("po", casePo);
		this.setAttribute("userName", User.getUserName());
		this.setAttribute("dtDate", DateUtil.format(new Date(), "yyyy年MM月dd日"));
		return "caseCheckNoticePage";
	}
	/*
	 * 提请审批操作
	 */
	public String addCheckNotice() throws AppException{
		String vcRecordId=this.getParameter("vcRecordId");
		String vcFileId=this.getParameter("fileId");
		String vcFileName=this.getParameter("fileName");
		String vcType=this.getParameter("vcType");
		WorkflowFlow flow=FlowWorkUtil.getWorkflowFlowByVcSeq(vcRecordId, "4");
		CaseSourceFiles file=new CaseSourceFiles();
		file.setVcId(GeneratorUUID.generateUUID());
		file.setDtDate(new Date());
		file.setVcDelFlag("0");
		file.setVcOrgId(User.getUserOrgId());
		file.setVcOrgName(User.getUserOrgName());
		file.setVcPersonId(User.getUserId());
		file.setVcPersonName(User.getUserName());
		file.setVcFileId(vcFileId);
		file.setVcFileName(vcFileName);
		file.setVcRecordId(vcRecordId);
		if(flow!=null)
			file.setVcFlowId(flow.getVcId());
		baselogic.addPo(file);
		return this.returnInfoForJS(true, "提请审批操作成功!");
	}
}
